package aclass.rocksea.com.database.db;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import aclass.rocksea.com.database.MyHScrollView;
import aclass.rocksea.com.database.R;

/**
 * Created by Mrw on 2017/7/6.
 *
 */

public class ScrollListActivity extends AppCompatActivity {

    private RelativeLayout mHead;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll);
        mHead = (RelativeLayout) findViewById(R.id.head);
        mHead.setFocusable(true);
        mHead.setClickable(true);
        mHead.setBackgroundColor(Color.parseColor("#b2d235"));
        mHead.setOnTouchListener(new ListViewAndHeadViewTouchListener());
        ListView mList = (ListView) findViewById(R.id.listView1);
        mList.setOnTouchListener(new ListViewAndHeadViewTouchListener());
        myAdapter = new MyAdapter(this, R.layout.item);
        mList.setAdapter(myAdapter);

    }

    public void add(View view){
        Toast.makeText(this, "O(∩_∩)O", Toast.LENGTH_SHORT).show();
    }

    private class ListViewAndHeadViewTouchListener implements View.OnTouchListener {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            //当在列头 和 listView控件上touch时，将这个touch的事件分发给 ScrollView
            HorizontalScrollView headSrcrollView = (HorizontalScrollView) mHead
                    .findViewById(R.id.MyHScrollView);
            headSrcrollView.onTouchEvent(arg1);
            return false;
        }
    }

    public class MyAdapter extends BaseAdapter {
        public List<ViewHolder> mHolderList = new ArrayList<>();

        int id_row_layout;
        LayoutInflater mInflater;

        public MyAdapter(Context context, int id_row_layout) {
            super();
            this.id_row_layout = id_row_layout;
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 15;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parentView) {
            ViewHolder holder;
            if (convertView == null) {
                synchronized (ScrollListActivity.this) {
                    convertView = mInflater.inflate(id_row_layout, null);
                    holder = new ViewHolder();

                    MyHScrollView scrollView1 = (MyHScrollView) convertView
                            .findViewById(R.id.MyHScrollView);
                    holder.scrollView = scrollView1;
                    holder.txt2 = (TextView) convertView
                            .findViewById(R.id.textView2);
                    holder.txt3 = (TextView) convertView
                            .findViewById(R.id.textView3);
                    holder.txt4 = (TextView) convertView
                            .findViewById(R.id.textView4);
                    holder.txt5 = (TextView) convertView
                            .findViewById(R.id.textView5);
                    holder.txt6 = (TextView) convertView.findViewById(R.id.textView6);
                    holder.txt7 = (TextView) convertView.findViewById(R.id.textView7);
                    holder.txt8 = (TextView) convertView.findViewById(R.id.textView8);
                    MyHScrollView headSrcrollView = (MyHScrollView) mHead
                            .findViewById(R.id.MyHScrollView);
                    headSrcrollView
                            .AddOnScrollChangedListener(new OnScrollChangedListenerImp(
                                    scrollView1));

                    convertView.setTag(holder);
                    mHolderList.add(holder);
                }
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.txt2.setText(position + "" + 2);
            holder.txt3.setText(position + "" + 3);
            holder.txt4.setText(position + "" + 4);
            holder.txt5.setText(position + "" + 5);
            holder.txt6.setText(position + "" + 6);
            holder.txt7.setText(position + "" + 7);
            holder.txt8.setText(position + "" + 8);

            return convertView;
        }
        class OnScrollChangedListenerImp implements MyHScrollView.OnScrollChangedListener {
            MyHScrollView mScrollViewArg;

            public OnScrollChangedListenerImp(MyHScrollView scrollViewar) {
                mScrollViewArg = scrollViewar;
            }
            @Override
            public void onScrollChanged(int l, int t, int oldl, int oldt) {
                mScrollViewArg.smoothScrollTo(l, t);
            }
        };
        class ViewHolder {
            TextView txt2;
            TextView txt3;
            TextView txt4;
            TextView txt5;
            TextView txt6;
            TextView txt7;
            TextView txt8;
            HorizontalScrollView scrollView;
        }

    }
}
